package com.sunzm.dynamic_rule.mock_data;

import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.RandomUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.util.ArrayList;
import java.util.List;

/**
 * HBase中的用户画像数据模拟，并插入HBase
 *
 * @author Administrator
 * @version 1.0
 * @date 2021-07-04 0:06
 *
 * 需要先在hbase中创建表：
 * create 'user-profile', 'f'
 */
public class MockProfileData {
    public static void main(String[] args) throws Exception {

        Configuration conf = HBaseConfiguration.create();
        conf.set("hbase.zookeeper.quorum", "192.168.1.158:2181");
        Connection conn = ConnectionFactory.createConnection(conf);
        Table table = conn.getTable(TableName.valueOf("user-profile"));

        List<Put> puts = new ArrayList<>();

        //随机生成10个用户的画像数据，插入 HBase
       /* for (int i = 0; i < 10; i++) {
            String account = StringUtils.leftPad(RandomStringUtils.randomNumeric(4), 4, "0");
            String deviceId = account;
            Put put = new Put(Bytes.toBytes(deviceId));
            put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("vip"), Bytes.toBytes("vip-" + RandomUtils.nextInt(1, 4)));
            put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("sex"), Bytes.toBytes(RandomUtils.nextInt(0, 2) + ""));
            put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("age"), Bytes.toBytes(RandomUtils.nextInt(18, 60) + ""));
            put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("cst"), Bytes.toBytes(RandomUtils.nextInt(0, 6000) + ""));
            puts.add(put);
        }*/

        //下面是手动插入
        String account =  "0075";
        Put put = new Put(Bytes.toBytes(account));
        put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("vip"), Bytes.toBytes("vip-1"));
        put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("sex"), Bytes.toBytes("1"));
        put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("age"), Bytes.toBytes("22"));
        put.addColumn(Bytes.toBytes("f"), Bytes.toBytes("cst"), Bytes.toBytes("2000"));
        puts.add(put);

        table.put(puts);
    }
}
